/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 25-sep-2012, 11:22:11
 */
package javaapplication26;

import entidades.Acceso;
import entidades.Conserjes;
import entidades.Funciones;
import entidades.Laboratorio;
import entidades.Profesor;
import entidades.Reserva;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Home
 */
public class Principal extends javax.swing.JFrame {

    Thread hilo ;
    String fin = "";
    /** Creates new form Principal */
    public Principal() {
        initComponents();
        
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jEImagePanel1 = new recursos.JEImagePanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Monitoriando General");

        jLabel2.setText("Fecha");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Iniciar Monitoreo");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Parar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Ocultar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setText("Puerto:");

        javax.swing.GroupLayout jEImagePanel1Layout = new javax.swing.GroupLayout(jEImagePanel1);
        jEImagePanel1.setLayout(jEImagePanel1Layout);
        jEImagePanel1Layout.setHorizontalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jEImagePanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jEImagePanel1Layout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(120, Short.MAX_VALUE))
        );
        jEImagePanel1Layout.setVerticalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  fin = "";
    hilo = new Thread(new Runnable() {

         
            public void run() {
                try {
                    Enumeration commports = CommPortIdentifier.getPortIdentifiers();
             CommPortIdentifier myCPI  = null ;
             Scanner mySC ;
             PrintStream myPS ;
             
            
             
             while(commports.hasMoreElements()){
                myCPI = (CommPortIdentifier) commports.nextElement();
                
               //System.out.println(myCPI.getName());
                
                if(myCPI.getName().equals(jTextField1.getText())){
                     System.out.println("lo ennnncontro");
                    break ;
                 }
                 
                 
                 
             }
             
             CommPort puerto = myCPI.open("Puerto Serial", 2000) ;
             SerialPort mySP = (SerialPort) puerto ;
             mySP.setSerialPortParams(19200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            // mySC = new Scanner(mySP.getInputStream());
             myPS = new PrintStream(mySP.getOutputStream());
             
             
              
          do{
              
               mySC = new Scanner(mySP.getInputStream());
               
             while(!mySC.hasNextLine() && !fin.equalsIgnoreCase("quit")){
             
                 mySC.close();
                 mySC = null ;
                 mySC = new Scanner(mySP.getInputStream());
             
             }
             
            if(!fin.equalsIgnoreCase("quit")){
             String valor = mySC.nextLine();
             myPS.println(valor);
             System.out.println(valor);
             Calendar c = new GregorianCalendar();
             Acceso a = new Acceso() ;
             
             a.setProfesor(Funciones.consultarProfesores("1010101010101010101010101010101010101010101010101010101010101010101010")) ;
             a.setLaboratorio(new Laboratorio("0", new Conserjes("0212321254"))) ;
             a.setHoraDeAcceso(c.getTime());
             
             Funciones.guardar(a);
             jTextArea1.setText( jTextArea1.getText() + c.getTime() + valor + "\n");
             // BUSCAR
             
             fin = valor ;
          }
              mySC.close();
               mySC = null ;
           
             
             
          }while(!fin.equalsIgnoreCase("quit"));
             myPS.close();
            myCPI = null ;
            //mySC.close();
            mySP.close();
            puerto.close();
                    System.out.println("fin del proceso");
                } catch (IOException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedCommOperationException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (PortInUseException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
         hilo.start();
        System.out.println("hilo iniciado");
    
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
     fin = "quit";
}//GEN-LAST:event_jButton2ActionPerformed

public Profesor consultar(String id){

    
  // Profesor p =  Funciones.consultarProfesores(id);
    id= "1010101010101010101010101010101010101010101010101010101010101010101010" ;
    List<Reserva> r =  Funciones.consultarProfesorReserva(id);
    
   
   
   
   
    
    return new Profesor();
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            // TODO add your handling code here:
                 Enumeration commports = CommPortIdentifier.getPortIdentifiers();
                     CommPortIdentifier myCPI  = null ;
                     Scanner mySC ;
                     PrintStream myPS ;
                     
                    
                     
                     while(commports.hasMoreElements()){
                        myCPI = (CommPortIdentifier) commports.nextElement();
                        
                       //System.out.println(myCPI.getName());
                        
                        if(myCPI.getName().equals("COM2")){
                             System.out.println("lo ennnncontro");
                            break ;
                         }
                         
                         
                         
                     }
                     
                     CommPort puerto = myCPI.open("Puerto Serial", 2000) ;
                     SerialPort mySP = (SerialPort) puerto ;
                     mySP.setSerialPortParams(19200,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    // mySC = new Scanner(mySP.getInputStream());
                     myPS = new PrintStream(mySP.getOutputStream());
                     
                     
                     String fin = "" ;
                //  do{
                      
                       mySC = new Scanner(mySP.getInputStream());
                       
                     while(!mySC.hasNextLine()){
                     
                         mySC.close();
                         mySC = null ;
                         mySC = new Scanner(mySP.getInputStream());
                     
                     }
                     
                    
                     String valor = mySC.nextLine();
                     myPS.println(valor);
                     System.out.println(valor);
                     fin = valor ;
                     
                     // mySC.close();
                     //  mySC = null ;
                    myPS.close();
                    myCPI = null ;
                    mySC.close();
                    mySP.close();
                    puerto.close();
                    
        } catch (IOException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedCommOperationException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PortInUseException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
         
}//GEN-LAST:event_jButton3ActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private recursos.JEImagePanel jEImagePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
